<template>
  <div class="menu-section">
    <div class="menu-buttons">
      <div
        class="menu-item"
        :class="{ active: currentRoute === '/' }"
        @click="switchMode('chat')"
      >
        <el-icon><ChatLineRound /></el-icon>
        <span class="menu-text">对话</span>
      </div>
      <div
        class="menu-item"
        :class="{ active: currentRoute === '/knowledge' }"
        @click="handleManagerClick"
      >
        <el-icon><Setting /></el-icon>
        <span class="menu-text">知识库管理</span>
      </div>
    </div>
  </div>
  <div>
    <transition name="fade">
      <SelectKnowledge
        v-show="currentMode === 'chat'"
        :isCollapsed="isCollapsed"
      />
    </transition>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";
import { useRouter, useRoute } from "vue-router";
import { ChatLineRound, Setting } from "@element-plus/icons-vue";
import SelectKnowledge from "./SelectKnowledge.vue";
import { useSidebarStore } from '../stores/sidebar';

const props = defineProps({
  isCollapsed: {
    type: Boolean,
    required: true,
  },
});

const sidebarStore = useSidebarStore();
const router = useRouter();
const route = useRoute();
const currentMode = ref("chat");
const currentRoute = ref("/");

onMounted(() => {
  currentRoute.value = route.path;
});

watch(
  () => route.path,
  (newPath) => {
    currentRoute.value = newPath;
    currentMode.value = newPath === "/" ? "chat" : "manager";
  }
);

const switchMode = (mode) => {
  currentMode.value = mode;
  router.push("/");
  // 保持展开状态
  sidebarStore.setExpanded(true);
};

const handleManagerClick = () => {
  currentMode.value = "manager";
  router.push("/knowledge");
  // 保持展开状态
  sidebarStore.setExpanded(true);
};
</script>

<style scoped>
.menu-section {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  margin: 8px;
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  color: #000;
}

.menu-item:hover {
  background-color: rgba(77, 107, 254, 0.1);
  color: #4d6bfe;
}

.menu-item.active {
  background-color: rgba(77, 107, 254, 1);
  color: #fff;
  font-weight: 500;
}

.menu-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
}

.menu-text {
  font-size: 14px;
  white-space: nowrap;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
